﻿using System;
using System.Windows;
using System.Windows.Media.Animation;
using HeritageAcademy.Models.DataModels;
using HeritageAcademy.Models.Loaders;
using HeritageAcademy.Models.ViewModels;
using System.Windows.Shell;

namespace HeritageAcademy.Library.Controls
{
    /// <summary>
    /// Navigator class.
    /// </summary>
    public partial class Navigator
    {
        #region Parameters

        private static SettingsConfigLoader _settingsConfigLoader;
        private static UsersConfigLoader _usersConfigLoader;

        #endregion

        #region Ctor

        public Navigator()
        {
            InitializeComponent();
            Current = this;
            _settingsConfigLoader = new SettingsConfigLoader(new Settings());
        }

        #endregion

        #region Properties 

        public static Navigator Current { get; private set; }

        public bool IsBookVisible { get; set; }

        #endregion Properties 

        #region Event Handlers 

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            ComeBackToLibrary();
        }

        /// <summary>
        /// Return Navigator to Library view.
        /// </summary>
        /// <remarks>Used in WindowContent so made into separate method</remarks>
        public void ComeBackToLibrary()
        {
            ShowLibrary();
            Course.DataContext = null;
        }

        #endregion

        #region Public Methods 

        public void ShowCourse(CourseViewModel course)
        {
            // TODO: implement book showing on startup
            _usersConfigLoader = new UsersConfigLoader(new Users());
            if (_usersConfigLoader.ActiveUser != null)
                _settingsConfigLoader.Model.GetSettingFromId(_usersConfigLoader.ActiveUser.Id).LastSelectedViewIsLibrary
                    = "false";
            _settingsConfigLoader.Save();

            Taskbar.AddToRecent(course.Model.ZipPath);
            Title.Text = course.Model.Title;
            Course.PrepareNavigate();

            // show the ReadingView
            var show = Resources["ShowBook"] as Storyboard;
            if (show == null) return;

            EventHandler handler = null;
            handler = delegate(object s, EventArgs e)
                          {
                              // remove the handler 
                              show.Completed -= handler;

                              // load ReadingView DataContext
                              Course.DataContext = course;

                              IsBookVisible = true;
                          };

            show.Completed += handler;
            show.Begin();
        }

        public void ShowCourseBoard(CourseViewModel course)
        {
            Library.DoExitAnimation();

            Title.Text = course.Model.Genre;

            CourseDetails.CourseTitle = course.Model.Title;
            CourseDetails.CourseDescription = course.Model.Description;
            CourseDetails.DataContext = course;

            // show the ReadingView
            var show = Resources["ShowCourseBoard"] as Storyboard;
            if (show == null) return;

            EventHandler handler = null;
            handler = delegate(object s, EventArgs e)
                          {
                              // remove the handler 
                              show.Completed -= handler;
                          };

            show.Completed += handler;
            show.Begin();

            // Force OptionsViewModel reinitialize current user and consequently styles
            Course.OptionsFlyout.SetParameters();
        }

        public void ShowTestBoard(CourseViewModel course)
        {
            Title.Text = course.Model.Title;

            TestWizard.FreezLastState();
            TestWizard.dtDurationTime.Stop();

            var tvm = new TestWizardViewModel(course);
            tvm.RequestClose += (s, e) => ShowTestBoard(course);

            TestBoard.DataContext = tvm;
            TestBoard.InitializeTestsList();

            // show TestBoard
            var show = Resources["ShowTestBoard"] as Storyboard;
            if (show == null) return;

            EventHandler handler = null;
            handler = delegate(object s, EventArgs e)
                          {
                              // remove the handler 
                              show.Completed -= handler;
                          };

            show.Completed += handler;
            show.Begin();
        }

        public void ShowTestWizard(TestWizardViewModel testWizardViewModel)
        {
            TestWizard.DataContext = testWizardViewModel;
            TestWizard.dtDurationTime.Start();

            // show TestWizard
            var show = Resources["ShowTestWizard"] as Storyboard;
            if (show == null) return;

            EventHandler handler = null;
            handler = delegate(object s, EventArgs e)
                          {
                              // remove the handler 
                              show.Completed -= handler;
                          };

            show.Completed += handler;
            show.Begin();
        }

        /// <summary>
        /// Show library when you come back from ReadingView.
        /// </summary>
        public void ShowLibrary()
        {
            _usersConfigLoader = new UsersConfigLoader(new Users());
            if (_usersConfigLoader.ActiveUser != null)
                _settingsConfigLoader.Model.GetSettingFromId(_usersConfigLoader.ActiveUser.Id).LastSelectedViewIsLibrary
                    = "true";
            _settingsConfigLoader.Save();

            // load ReadingView DataContext
            Course.DataContext = null;

            // show the ReadingView
            var show = Resources["ShowLibrary"] as Storyboard;
            if (show == null) return;

            EventHandler handler = null;
            handler = delegate(object s, EventArgs e)
                          {
                              show.Completed -= handler;
                              Library.DoEntranceAnimation();
                              IsBookVisible = false;
                          };

            show.Completed += handler;
            show.Begin();
        }

        /// <summary>
        /// Show library on start up.
        /// </summary>
        /// <param name="library">Library view model object</param>
        public void ShowLibrary(LibraryViewModel library)
        {
            Library.DataContext = library;
        }

        #endregion Public Methods 
    }

    static class Taskbar
    {
        public static void AddToRecent(string destination)
        {
            var jumpTask = new JumpTask
            {
                Title = "Курс",
                ApplicationPath = destination,
                CustomCategory = "Недавние",
                IconResourcePath = @"C:\Windows\System32\imageres.dll",
                IconResourceIndex = 14
            };

            var jumpList = JumpList.GetJumpList(Application.Current);
            jumpList.JumpItems.Add(jumpTask);
            jumpList.Apply();
        }
    }

    internal interface INavigable
    {
        void PrepareNavigate();
    }
}